cmake_minimum_required (VERSION 3.10)

project(example_cyber)

find_package(cyber-rt QUIET)
if(cyber-rt_FOUND)
    message(STATUS "build ${PROJECT_NAME}")
    file(GLOB MY_PROTO_FILES
        "${CMAKE_CURRENT_LIST_DIR}/proto/*.proto"
    )

    find_program(PROTOC_COMPILER NAMES protoc 
        PATHS /usr/local/bin)

    foreach(FIL ${MY_PROTO_FILES})
        message(STATUS "generate proto file: ${FIL}")
        get_filename_component(FIL_WE ${FIL} NAME_WE)
        execute_process(
            COMMAND ${PROTOC_COMPILER}
            -I ${CMAKE_CURRENT_LIST_DIR}/proto
            --cpp_out=${CMAKE_CURRENT_LIST_DIR}/proto
            "${FIL_WE}.proto"
        )
    endforeach()

    file(GLOB MY_PROTO_SRCS "proto/*.pb.cc")

    link_directories("/opt/local/cyber-rt/lib")

    aux_source_directory(. DIR_SOURCES)
    add_executable (${PROJECT_NAME} ${DIR_SOURCES} ${MY_PROTO_SRCS})

    set(CYBER_LIBRARIES cyber)

    target_link_libraries(${PROJECT_NAME} pthread)
    target_link_libraries(${PROJECT_NAME} glog)
    target_link_libraries(${PROJECT_NAME} gflags)
    target_link_libraries(${PROJECT_NAME} fastcdr)
    target_link_libraries(${PROJECT_NAME} fastrtps)
    target_link_libraries(${PROJECT_NAME} protobuf)
    target_link_libraries(${PROJECT_NAME} ${CYBER_LIBRARIES})
    
endif()